/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.hamacekj.geometr;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.Map;
import javax.swing.JList;
import javax.swing.ListModel;

/**
 * Nástroj na tisk postupu konstrukce.
 * @author hamacekh
 */
public class PrintStepList implements Printable {

    private JList<? extends Step> list;
    private ListModel<? extends Step> model;
    private ListStepRenderer renderer;

    
    public PrintStepList(JList<? extends Step> list, Map<String, UIConstructionObject> plugins) {
        this.list = list;
        this.renderer = new ListStepRenderer(plugins);
        this.model = list.getModel();
    }
    
    
    
    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if(pageIndex > 0){
            return Printable.NO_SUCH_PAGE;
        }
        Graphics2D gr = (Graphics2D)graphics;
        gr.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        for(int i = 0; i < model.getSize(); i++){
            ListStep current = renderer.getRenderComponent(list, model.getElementAt(i), i, false, false);
            current.paint(gr);
            gr.translate(0, current.getPreferredSize().height);
        }
        return Printable.PAGE_EXISTS;
    }
    
}
